package com.ke.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ke.reggie.common.CustomException;
import com.ke.reggie.common.R;
import com.ke.reggie.entity.Category;
import com.ke.reggie.entity.Dish;
import com.ke.reggie.entity.Setmeal;
import com.ke.reggie.mapper.CategoryMapper;
import com.ke.reggie.service.CategoryService;
import com.ke.reggie.service.DishService;
import com.ke.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * @BelongsProject: reggie_take_out
 * @BelongsPackage: com.ke.reggie.service.impl
 * @Author: ke_huifeng
 * @CreateTime: 2022-10-05  14:18
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Override
    public boolean removeById(Serializable id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        if (dishService.count(dishLambdaQueryWrapper) > 0){
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        if (setmealService.count(setmealLambdaQueryWrapper) > 0){
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        return super.removeById(id);
    }
}
